import os


class TextProcessor:
    def __init__(self):
        # 初始化的时候加载plugins
        self.load_plugins()

    def load_plugins(self):
        # 从plugins文件夹中, 加载所有plugins.py文件并运行
        for filename in os.listdir("plugins"):
            if not filename.endswith('.py') or filename.startswith('_'):
                continue
            self.run_plugin(filename)

    def run_plugin(self, filename):
        # 提取插件的文件名
        plugin_name = os.path.splitext(filename)[0]
        # 通过__import__()函数动态的导入插件
        plugin = __import__("plugins." + plugin_name, fromlist=[plugin_name])
        # 运行插件的run函数
        plugin.run(self)

    def work(self, plugin):
        # 给出对应的plugin工作的目标提示
        print(f"plugin is processing the text into {plugin}...")


if __name__ == '__main__':
    processor = TextProcessor()
    # 调用对应的plugin, 对目标text进行处理, 并返回处理后的结果
    # TODO 这种动态增加的属性调用时怎么消除这种IDE错误提示?
    a = processor.plugin1("test text")
    b = processor.plugin2('abc')
    print(a)
    print(b)
